package com.zhiyou100.handler;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义处理异常的 Handler
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-09-02 20:59
 * <p>
 * // @Component 需要扫描
 */

@Component
public class MyExceptionHandler implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {


        final ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("errorMessage", ex.getMessage());

        modelAndView.setViewName("404");

        return modelAndView;

    }
}
